package com.compomics.prioritizer.db.accessors;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: niels
 * Date: 28/07/11
 * Time: 15:30
 * To change this template use File | Settings | File Templates.
 */
public class Instrument_to_user extends Instrument_to_userTableAccessor {

    public Instrument_to_user(){
        super();
    }

    public Instrument_to_user(HashMap aHashMap){
        super(aHashMap);
    }

    public static ArrayList<Long> getUserIdsByInstumentId(Connection aConn, Long aInstrumentId) throws SQLException {
        PreparedStatement prep = aConn.prepareStatement("select l_userid from instrument_to_user where l_instrumentid = ?");
        prep.setLong(1, aInstrumentId);
        ResultSet rs = prep.executeQuery();

        ArrayList<Long> aUserIdsList = new ArrayList<Long>();
        while (rs.next()){
            aUserIdsList.add(rs.getLong(1));
        }

        return aUserIdsList;
    }

    public static User[] getOperatorsByInstrumentId(Connection aConn, Long aInstrumendId) throws SQLException {
        List<Long> lInstrumentUserIdsList = getUserIdsByInstumentId(aConn, aInstrumendId);
        User[] lInstrumentUsersArray = new User[lInstrumentUserIdsList.size()];
        int lIndex = 0;
        for(Long l : lInstrumentUserIdsList){
             lInstrumentUsersArray[lIndex] = User.getUserById(aConn, l);
        }

        return lInstrumentUsersArray;
    }

}
